IF EXISTS (SELECT * FROM sysobjects WHERE xtype = 'P' AND name = 'sp_GenerateInsertScript')
    BEGIN
         DROP PROCEDURE sp_GenerateInsertScript
    END
    GO

    SET ANSI_NULLS ON
    GO

    SET QUOTED_IDENTIFIER ON
    GO

    CREATE PROCEDURE [dbo].[sp_GenerateInsertScript] 
     @TableName SYSNAME  
    ,@SchemaName SYSNAME ='dbo' 
    ,@Condition VARCHAR(500) = ''  
    AS 
 
    SET NOCOUNT ON 
 
    DECLARE @ColumnList VARCHAR(MAX) 
    DECLARE @InsertList VARCHAR(MAX) 
    DECLARE @pkcolList VARCHAR(MAX)
    DECLARE @SQL VARCHAR(MAX)
    DECLARE @WhereSQL VARCHAR(MAX)
 
    SELECT @ColumnList = '' 
    SELECT @InsertList = '' 
    SELECT @pkcolList  = ''  
 
    SELECT @ColumnList = UPPER(@ColumnList) + CASE  
                                       WHEN ST.name not in ('timestamp','geography','geometry','hierarchyid','image','binary','varbinary') THEN Quotename(SC.name) + ','  
                                       ELSE ''  
                                       END  
          ,@InsertList = UPPER(@InsertList) + CASE WHEN ST.name not in ('timestamp','geography','geometry','hierarchyid','image','binary','varbinary') THEN 
                                        '''' +  QUOTENAME(sc.name) + '='' + '  + 'CASE WHEN ' + Quotename(SC.name)  
                                        + ' IS NULL THEN ''NULL'' ELSE  '''''''' +    REPLACE(CONVERT(VARCHAR(MAX),' + Quotename(SC.name) + '),'''''''','''''''''''') + ''''''''  END + ' + ' '','' + ' 
                                       ELSE '' 
                                       END 
      FROM SYS.objects so 
      JOIN SYS.columns sc 
        ON so.object_id = sc.object_id 
      JOIN SYS.types st 
        ON sc.user_type_id = st.user_type_id 
       AND sc.system_type_id = st.system_type_id 
      JOIN SYS.schemas sch 
        ON sch.schema_id = so.schema_id 
     WHERE SO.name = @TableName  
       AND sch.name = @SchemaName 
     ORDER BY SC.column_id 
 
 
    SELECT @WhereSQL = CASE  
                       WHEN LTRIM(RTRIM(@Condition)) <> '' THEN ' WHERE ' + @Condition 
                       ELSE '' 
                       END 
 
    SELECT @ColumnList =UPPER(SUBSTRING(@ColumnList,1,LEN(@ColumnList)-1) )
    SELECT @InsertList =UPPER(SUBSTRING(@InsertList,1,LEN(@InsertList)-4) + '''' )
     
    SELECT @SQL = 'SELECT ''INSERT INTO ' + QUOTENAME(@SchemaName) + '.' + QUOTENAME(@Tablename) +  char(10)+  '(' + @ColumnList + ')'' + char(10)+  ''SELECT '' + ' + @InsertList  
                + ' FROM ' + QUOTENAME(@SchemaName) + '.' + QUOTENAME(@Tablename)  
                + @WhereSQL 
     
    --PRINT @SQL 
    EXEC(@SQL) 
    GO